/**
 * Copyright (C) 2013 Denatics Development
 * 
 * Programmed by David Tiersch <dtiersch@denatics.com>
 * 
 * This file is part of B0mberman.
 * 
 * B0mberman is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * B0mberman is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with B0mberman. If not, see <http://www.gnu.org/licenses/>.
 */
package com.denatics.bomberman.gui;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.denatics.bomberman.tools.Debugger;
import com.denatics.bomberman.tools.Debugger.LEVEL;

/**
 * This class loads the Images that are used throughout the game.
 * 
 * @author dtiersch
 */
public final class Images {

    /**
     * The Image with the basic textures for the entities.
     */
    public static final Bitmap TEXTURE = loadFromFile("tex.png");

    /**
     * The Image with the basic textures for the sprites.
     */
    public static final Bitmap SPRITES = loadFromFile("sprites.png");

    /**
     * Color used for transparency.
     */
    private static final int TRANSPARENCY = 0xffff00ff;

    /**
     * Constant to remove the Alpha-Channel.
     */
    private static final int COLORSHIFT = 0x00ffffff;

    /**
     * Empty, private constructor to forbid Creating an Instance of this class.
     */
    private Images() {
        // do nothing
    }

    /**
     * Loads a Image from a file.
     * 
     * @param fileName
     *            name of the File in the resource folder
     * 
     * @return the loaded Image
     */
    public static Bitmap loadFromFile(final String fileName) {
        try {
            final BufferedImage image =
                    ImageIO.read(Images.class.getResource("/res/" + fileName));
            Debugger.printInfo(LEVEL.STARTUP, "Read file /res/" + fileName);

            final Bitmap bitmap =
                    new Bitmap(image.getWidth(), image.getHeight());
            image.getRGB(0, 0, image.getWidth(), image.getHeight(),
                    bitmap.getPixels(), 0, image.getWidth());

            for (int i = 0; i < bitmap.getPixels().length; i++) {
                final int pixel = bitmap.getPixels()[i];
                int color = pixel & COLORSHIFT;
                if (pixel == TRANSPARENCY) {
                    color = -1;
                }
                bitmap.getPixels()[i] = color;
            }

            return bitmap;
        } catch (IOException e) {
            Debugger.printError(fileName + " not found!");
            throw new RuntimeException(e);
        }

    }
}
